/*
 *    Copyright 2009-2025 the original author or authors.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       https://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package org.apache.ibatis.scripting.xmltags;

import org.apache.ibatis.builder.SqlSourceBuilder;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.SqlSource;
import org.apache.ibatis.reflection.ParamNameResolver;
import org.apache.ibatis.session.Configuration;

/**
 * 负责处理动态SQL语句,,,解析动态SQL
 * @author Clinton Begin
 */
public class DynamicSqlSource implements SqlSource {

  private final Configuration configuration;
  // sqlNode树的根节点
  private final SqlNode rootSqlNode;

  private final ParamNameResolver paramNameResolver;

  public DynamicSqlSource(Configuration configuration, SqlNode rootSqlNode) {
    this(configuration, rootSqlNode, null);
  }

  public DynamicSqlSource(Configuration configuration, SqlNode rootSqlNode, ParamNameResolver paramNameResolver) {
    this.configuration = configuration;
    this.rootSqlNode = rootSqlNode;
    this.paramNameResolver = paramNameResolver;
  }

  /**
   *
   * @param parameterObject  用户传入的实参
   * @return
   */
  @Override
  public BoundSql getBoundSql(Object parameterObject) {
    // 创建DynamicContext对象
    DynamicContext context = new DynamicContext(configuration, parameterObject, null, paramNameResolver, true);
    // 组合模式，， 递归调用SqlNode的apply方法，解析动态SQL
    rootSqlNode.apply(context);
    // 获取到完整的sql
    String sql = context.getSql();
    // 创建SqlSourceBuilder，，解析参数属性，，，将 #{}替换成 ？
    SqlSource sqlSource = SqlSourceBuilder.buildSqlSource(configuration, sql, context.getParameterMappings());
    // 创建BoundSql对象，将DynamicContext.bindings中的参数信息复制到其 additionalParameters集合中保存
    BoundSql boundSql = sqlSource.getBoundSql(parameterObject);
    context.getBindings().forEach(boundSql::setAdditionalParameter);
    return boundSql;
  }

}
